package com.sunlee.app.api.controller;

import com.sunlee.app.api.annotation.Login;
import com.sunlee.common.core.entity.PageData;
import com.sunlee.common.core.entity.Result;
import com.sunlee.common.core.enums.ErrorCodeEnum;
import com.sunlee.common.core.exception.BusinessException;
import com.sunlee.common.service.dto.ProductActionDTO;
import com.sunlee.common.service.dto.ProductDTO;
import com.sunlee.common.service.dto.ProductListDTO;
import com.sunlee.common.service.dto.req.ProductWaterfallsFlowDTO;
import com.sunlee.common.service.enums.StatusEnum;
import com.sunlee.common.service.service.ProductService;
import com.sunlee.common.service.service.UserActionService;
import com.sunlee.app.api.context.UserContext;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import springfox.documentation.annotations.ApiIgnore;

import java.util.Collections;
import java.util.Map;
import java.util.Objects;

/**
 * @author sunjulei
 * @date 2022/7/2
 */
@Slf4j
@RestController
@RequestMapping("product")
public class ProductController {

    @Autowired
    private ProductService productService;

    @Autowired
    private UserActionService userActionService;

    @PostMapping("page")
    public Result<PageData<ProductDTO>> homeWaterfallsFlow(@Validated @RequestBody ProductWaterfallsFlowDTO request) {
        return Result.ok(productService.homeWaterfallsFlow(request));
    }

    /**
     * 根据商品ID查询商品详情
     *
     * @param id 商品ID
     * @return 商品详情
     */
    @GetMapping("info/{id}")
    public Result<ProductDTO> getInfo(@PathVariable Long id) {

        ProductDTO productDTO = productService.getOwn(id);

        if (Objects.isNull(productDTO) || productDTO.getStatus() == StatusEnum.INVALID.getCode()) {
            throw new BusinessException(ErrorCodeEnum.RESOURCES_REMOVED);
        }

        Map<Long, ProductActionDTO> productMap = userActionService.getProductActionDataByIds(Collections.singletonList(id));
        productDTO.setActionData(productMap.get(id));

        return Result.ok(productDTO);
    }


    /**
     * 搜索商品
     *
     * @return 商品列表
     */
    @GetMapping("search")
    public Result<PageData<ProductListDTO>> searchPage(@ApiIgnore @RequestParam Map<String, Object> params) {
        log.info("searchRes={}", params.get("keyword"));
        return Result.ok(productService.searchPage(params));
    }

    /**
     * 根据商品ID下载
     *
     * @param pId 商品ID
     * @return 下载地址
     */
    @Login
    @GetMapping("download/{pId}")
    public Result<String> download(@PathVariable Long pId) {
        return Result.ok(userActionService.downloadById(UserContext.getUserId(), pId));
    }

    /**
     * 收藏/取消收藏 商品
     *
     * @param pId 商品ID
     * @return 是否成功
     */
    @Login
    @GetMapping("collection/{pId}/{status}")
    public Result<Boolean> collection(@PathVariable("pId") Long pId, @PathVariable("status") Integer status) {
        if (Objects.isNull(status) || Objects.isNull(StatusEnum.value(status))) {
            return Result.error(ErrorCodeEnum.PARAM_IS_INVALID.format("status"));
        }
        return Result.ok(userActionService.collectionById(UserContext.getUserId(), pId, status));
    }
}
